<template>
  <header class="header">
    <h1>todos</h1>
    <input v-model="isAll" id="toggle-all" class="toggle-all" type="checkbox" />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model="val"
      @keyup.enter="addBtn"
    />
  </header>
</template>

<script>
export default {
  props: {
    list: Array,
  },
  data() {
    return {
      val: "",
    };
  },
  methods: {
    addBtn() {
      if (this.val.trim().length === 0) return alert("不能为空");
      this.list.push({
        id: 100 + this.list.length,
        name: this.val,
        isDone: false,
      });
      this.val = "";
    },
  },
  computed: {
    isAll: {
      set(flag) {
        return this.list.forEach((item) => (item.isDone = flag));
      },
      get() {
        return this.list.every((item) => item.isDone === true);
      },
    },
  },
};
</script>
